package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ClassInstructorTeacherMapper;
import com.zhentao.mapper.ClassesMapper;
import com.zhentao.mapper.InstructorMapper;
import com.zhentao.mapper.TeacherMapper;
import com.zhentao.pojo.ClassInstructorTeacher;
import com.zhentao.pojo.Classes;
import com.zhentao.pojo.Instructor;
import com.zhentao.pojo.Teacher;
import com.zhentao.service.TeacherService;
import com.zhentao.vo.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author hp
* @description 针对表【teacher】的数据库操作Service实现
* @createDate 2025-07-09 15:28:47
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService{
    @Autowired
    private ClassInstructorTeacherMapper classInstructorTeacherMapper;
    @Autowired
    private ClassesMapper classesMapper;
    @Autowired
    private InstructorMapper instructorMapper;
    @Override
    public Page<Teacher> pageTeacherWithClassesAndInstructors(Query query) {
        // 创建分页对象
        Page<Teacher> page = new Page<>(query.getCurrent(), query.getSize());

        // 构建查询条件
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();

        // 按讲师姓名模糊查询
        if (StringUtils.hasText(query.getTeacherName())) {
            wrapper.like(Teacher::getName, query.getTeacherName());
        }

        // 按性别查询
        if (query.getSex() != null) {
            wrapper.eq(Teacher::getSex, query.getSex());
        }

        // 执行分页查询
        Page<Teacher> teacherPage = this.page(page, wrapper);

        // 为每个讲师查询班级和导员信息
        for (Teacher teacher : teacherPage.getRecords()) {
            // 1. 查询该讲师所带班级的关联关系
            List<ClassInstructorTeacher> relations = classInstructorTeacherMapper
                    .selectList(new QueryWrapper<ClassInstructorTeacher>()
                            .eq("teacher_id", teacher.getId()));
            if (relations == null) relations = new ArrayList<>();

            // 2. 提取班级ID列表
            List<Integer> classIds = relations.stream()
                    .map(ClassInstructorTeacher::getClassId)
                    .collect(Collectors.toList());

            // 3. 查询班级信息
            List<Classes> classList = new ArrayList<>();
            if (!classIds.isEmpty()) {
                classList = classesMapper.selectBatchIds(classIds);
            }
            if (classList == null) classList = new ArrayList<>();

            // 4. 设置讲师的班级列表
            teacher.setClasses(classList);

            // 5. 查询搭班导员信息（该讲师所带班级的所有导员）
            List<Instructor> partnerInstructors = new ArrayList<>();
            if (!classIds.isEmpty()) {
                List<Integer> instructorIds = relations.stream()
                        .map(ClassInstructorTeacher::getInstructorId)
                        .distinct()
                        .collect(Collectors.toList());

                if (!instructorIds.isEmpty()) {
                    partnerInstructors = instructorMapper.selectBatchIds(instructorIds);
                }
            }
            if (partnerInstructors == null) partnerInstructors = new ArrayList<>();
            teacher.setPartnerInstructors(partnerInstructors);
        }

        return teacherPage;
    }

    @Override
    public Teacher getTeacherWithClassesAndInstructors(Integer teacherId) {
        // 查询讲师基本信息
        Teacher teacher = this.getById(teacherId);
        if (teacher == null) {
            return null;
        }

        // 查询该讲师所带班级的关联关系
        List<ClassInstructorTeacher> relations = classInstructorTeacherMapper
            .selectList(new QueryWrapper<ClassInstructorTeacher>()
                .eq("teacher_id", teacherId));

        // 提取班级ID列表
        List<Integer> classIds = relations.stream()
            .map(ClassInstructorTeacher::getClassId)
            .collect(Collectors.toList());

        // 查询班级信息
        List<Classes> classList = new ArrayList<>();
        if (!classIds.isEmpty()) {
            classList = classesMapper.selectBatchIds(classIds);
        }

        // 为每个班级查询对应的导员信息
        for (Classes classes : classList) {
            // 找到该班级对应的关联关系
            ClassInstructorTeacher relation = relations.stream()
                .filter(r -> r.getClassId().equals(classes.getId()))
                .findFirst()
                .orElse(null);


        }

        // 设置讲师的班级列表
        teacher.setClasses(classList);

        // 查询搭班导员信息（该讲师所带班级的所有导员）
        List<Instructor> partnerInstructors = new ArrayList<>();
        if (!classIds.isEmpty()) {
            // 查询所有与该讲师搭班的导员ID（去重）
            List<Integer> instructorIds = relations.stream()
                .map(ClassInstructorTeacher::getInstructorId)
                .distinct()
                .collect(Collectors.toList());

            if (!instructorIds.isEmpty()) {
                partnerInstructors = instructorMapper.selectBatchIds(instructorIds);
            }
        }
        teacher.setPartnerInstructors(partnerInstructors);

        return teacher;
    }
}




